package com.zwk.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwk.base.handler.GuLiException;
import com.zwk.edu.client.VodClient;
import com.zwk.edu.entity.Video;
import com.zwk.edu.mapper.VideoMapper;
import com.zwk.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author zwk
 * @since 2022-08-22
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Qualifier("com.zwk.edu.client.VodClient")
    @Autowired
    private VodClient vodClient;

    // 根据课程id删除小节
    @Override
    public void removeByCourseId(String courseId) {
        // 根据课程id查询视频id
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoQueryWrapper.select("video_source_id"); // 只查询video_source_id
        List<Video> videos = videoMapper.selectList(videoQueryWrapper);
        if (videos != null){
            List<String> ids = new ArrayList<>();
            for (Video video: videos
                 ) {
                if (ObjectUtils.isNotEmpty(video.getVideoSourceId())){
                    ids.add(video.getVideoSourceId());
                }
            }
            if (ids.size() > 0){
                vodClient.deleteBatch(ids);
            }
        }

        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        videoMapper.delete(wrapper);
    }
}
